//
//  DynamicColumnFieldCore+ChoicePhoto.m
//  
//
//  Created by wjxfhxy on 15/12/30.
//
//

#import "DynamicColumnFieldCore+ChoicePhoto.h"

#import "DynamicColumnFieldCore+Internal.h"

@interface DynamicColumnFieldCore(ChoicePhotoDelegate)<UIImagePickerControllerDelegate,UINavigationControllerDelegate,DoImagePickerControllerDelegate>

@end

@implementation DynamicColumnFieldCore (ChoicePhoto)

-(void)choicePhoto:(NSUInteger)maxCount
{
    [UIViewController closeKeyBoard];
    
    if ([XMSFileHelper getSystemFreeSize] / 1024 / 1024 < 150)
    {
        [UIAlertView showInfoMsg:@"存储空间不足，未能获取图片"];
        return;
    }
    
//#if !TARGET_IPHONE_SIMULATOR
    [XUIActionSheetView actionSheetViewWithTitle:@"t" cancelButtonTitle:@"取消" otherButtonTitles:@[@"拍照",@"从相册选择"] show:^(NSUInteger clickIndex)
     {
         //if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
         {
             if(clickIndex == 0)
             {
                 if (![SystemRightJudge checkCameraAuthorization])
                     return;
                 
                 UIImagePickerController *controller = [UIImagePickerController new];
                 controller.delegate = self;
                 controller.sourceType = UIImagePickerControllerSourceTypeCamera;
                 [UIViewController presentViewController:controller animated:YES completion:^{}];
             }
             else
             {
                 if (![SystemRightJudge checkPhotoAuthorization])
                     return;
                 
                 DoImagePickerController *controller = [DoImagePickerController new];
                 controller.nMaxCount = maxCount;
                 controller.nColumnCount = 4;
                 controller.delegate = self;
                 [UIViewController presentViewController:controller animated:YES completion:^{}];
             }
         }
     }];
//#else
//    DoImagePickerController *controller = [DoImagePickerController new];
//    controller.nMaxCount = maxCount;
//    controller.nColumnCount = 4;
//    controller.delegate = self;
//    [UIViewController presentViewController:controller animated:YES completion:^{}];
//#endif
}

- (void)imageOperation:(NSArray *)newImages completion:(void(^)(UIImage *operatedImage,NSString *imagePath))completion allCompletion:(void(^)())allCompletion
{
    DispatchGlobalThreadAsync(^
    {
        [_delegate showProgressView:@"图片处理中"];
        
        for(UIImage *newImage in newImages)
        {
            __block UIImage *operatedImage = nil;
            NSString *guid = [XMSGUID GUIDLowString];
            NSString *filename = [NSString stringWithFormat:@"%@.jpg", guid];
            NSString *path = [[XMSFileHelper tempPath] stringByAppendingPathComponent:filename];
            [UIImageJPEGRepresentation(newImage, 1) writeToFile:path atomically:NO];
            [UIImage imageFileSize:[XMSFileHelper getFileSize:path] imageSize:newImage.size compressParam:^(CGFloat zoomMultiple,CGSize zoomSize,CGFloat quality)
             {
                 CGSize size = zoomSize;
                 operatedImage = [UIImage imageFromImage:newImage scaledToSize:size];
                 
                 //保存原始照片到本地
                 NSData *data = UIImageJPEGRepresentation(operatedImage, quality);
                 [data writeToFile:path atomically:NO];
             }];
            
            DispatchMainThreadAsync(^
            {
                completion(operatedImage,path);
            });
        }
        
        DispatchMainThreadAsync(^
        {
            allCompletion();
        });
        
        [_delegate hideProgressView];
    });
}

- (void)imageOperation:(NSArray *)newImages
{
    [self imageOperation:newImages completion:^(UIImage *operatedImage,NSString *imagePath)
    {
        if([_selectedCell isMemberOfClass:[HeadImageGroupTableViewCell class]])
        {
            _headImagePath = imagePath;
            
            ((HeadImageGroupTableViewCell*)_selectedCell).headImageView.image = [UIImage imageWithContentsOfFile:imagePath];
        }
        else if([_selectedCell isMemberOfClass:[ImagePanelGroupTableViewCell class]])
        {
            [((ImagePanelGroupTableViewCell*)_selectedCell) addImage:operatedImage withTag:imagePath];
        }
        else if([_selectedCell isMemberOfClass:[MixedPanelGroupTableViewCell class]])
        {
            [((MixedPanelGroupTableViewCell*)_selectedCell) addImage:operatedImage withTag:imagePath];
        }
        
    } allCompletion:^
    {
        _selectedCell = nil;
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [UIViewController dismissViewControllerAnimated:YES completion:^{}];
    
    [self imageOperation:@[image]];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [UIViewController dismissViewControllerAnimated:YES completion:^{}];
}

-(void)didSelectPhotosFromDoImagePickerController:(DoImagePickerController *)picker result:(NSArray *)aSelected
{
    [UIViewController dismissViewControllerAnimated:YES completion:^{}];
    
    [self imageOperation:aSelected];
}

-(void)didCancelDoImagePickerController
{
    [UIViewController dismissViewControllerAnimated:YES completion:^{}];
}


-(void)choicePhotoWithMaxCount:(NSUInteger)maxCount sourceType:(UIImagePickerControllerSourceType)sourceType
{
    [UIViewController closeKeyBoard];
    
    if ([XMSFileHelper getSystemFreeSize] / 1024 / 1024 < 150)
    {
        [UIAlertView showInfoMsg:@"存储空间不足，未能获取图片"];
        return;
    }
    
#if !TARGET_IPHONE_SIMULATOR
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
    {
        if(sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
        {
            if (![SystemRightJudge checkPhotoAuthorization])
                return;
            
            DoImagePickerController *controller = [DoImagePickerController new];
            controller.nMaxCount = maxCount;
            controller.nColumnCount = 4;
            controller.delegate = self;
            [UIViewController presentViewController:controller animated:YES completion:NULL];
        }
        else if(sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            if (![SystemRightJudge checkCameraAuthorization])
                return;
            
            UIImagePickerController *controller = [UIImagePickerController new];
            controller.delegate = self;
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            [UIViewController presentViewController:controller animated:YES completion:NULL];
        }
    }
    else
    {
        
    }
#else
    if(sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
    {
        DoImagePickerController *controller = [DoImagePickerController new];
        controller.nMaxCount = maxCount;
        controller.nColumnCount = 4;
        controller.delegate = self;
        [UIViewController presentViewController:controller animated:YES completion:NULL];
    }
    else if(sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        [self imageOperation:@[[UIImage imageWithMainBundle:@"Icon-120.png"]]];
    }
#endif
}

@end
